package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.em.model.MaintainStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.MaintainStrategyDao;
import cn.b.sky.mes.em.enums.MaintainTaskStatus;
import cn.b.sky.mes.em.enums.StrategyType;
import cn.b.sky.mes.em.model.condition.MaintainStrategyCondition;
import cn.b.sky.mes.em.service.MaintainStrategyService;
import cn.b.sky.mes.em.service.MaintainTaskService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("maintainStrategyService")
public class MaintainStrategyServiceImpl implements MaintainStrategyService {

  private static final Logger LOG = LoggerFactory.getLogger(MaintainStrategyServiceImpl.class);

  @Resource
  MaintainStrategyDao maintainStrategyDao;

  @Resource
  MaintainTaskService maintainTaskService;

  @Override
  public List<MaintainStrategy> selectMaintainStrategyByCondition(MaintainStrategyCondition maintainStrategyCondition) {
    return maintainStrategyDao.selectMaintainStrategyByCondition(maintainStrategyCondition);
  }

  @Override
  public int selectMaintainStrategyCount(MaintainStrategyCondition maintainStrategyCondition) {
    return maintainStrategyDao.selectMaintainStrategyCount(maintainStrategyCondition);
  }

  @Override
  public MaintainStrategy selectMaintainStrategyByCode(String siteCode, String strategyCode) {
    return maintainStrategyDao.selectMaintainStrategyByCode(siteCode, strategyCode);
  }

  @Override
  public void insertMaintainStrategy(MaintainStrategy maintainStrategy) {
    maintainStrategyDao.insertMaintainStrategy(maintainStrategy);
  }

  @Override
  public void updateMaintainStrategy(MaintainStrategy maintainStrategy) {
    //如果关键元素发生更改 则删除已产生但未执行的任务
    MaintainStrategy oldStrategy = maintainStrategyDao.selectMaintainStrategyByCode(maintainStrategy.getSiteCode(),
        maintainStrategy.getStrategyCode());
    maintainStrategy.setStrategyTypeECode(oldStrategy.getStrategyTypeECode());
    if (maintainStrategy.getStrategyTypeECode().equals(StrategyType.CYCLE.getValue())) {
      if (maintainStrategy.getStrategyTimeValue() != oldStrategy.getStrategyTimeValue()
          || !maintainStrategy.getTimeUnitECode().equals(oldStrategy.getTimeUnitECode())) {
        maintainTaskService.deleteMaintainTaskByStatus(maintainStrategy.getSiteCode(), null,
            maintainStrategy.getStrategyCode(), MaintainTaskStatus.PLAN.getValue());
      }
    } else {
      if (maintainStrategy.getStrategyCountValue() != oldStrategy.getStrategyCountValue()) {
        maintainTaskService.deleteMaintainTaskByStatus(maintainStrategy.getSiteCode(), null,
            maintainStrategy.getStrategyCode(), MaintainTaskStatus.PLAN.getValue());
      }
    }
    maintainStrategyDao.updateMaintainStrategy(maintainStrategy);

  }

  @Override
  public BatchJobReport deleteMaintainStrategy(String siteCode, String strategyCode) {
    String cause = "";
    Boolean success = true;
    try {
      maintainStrategyDao.deleteMaintainStrategy(siteCode, strategyCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(strategyCode, cause, success);
  }

  @Override
  public List<MaintainStrategy> selectMaintainStrategyForSuggest(MaintainStrategyCondition maintainStrategyCondition) {
    return maintainStrategyDao.selectMaintainStrategyForSuggest(maintainStrategyCondition);
  }

}
